So, updating my on here, I found the problem(s), and because this form is a learning tool, I decided to document them here. So the first one, where it wasn't printing anything. After coming back and looking for half another half hour, I traced the problem back to the if statement that checks whether or not length was returned as -1, using this statement
Code:
if(length = -1) //If end of file is reached
return 0; //return 0.
Now, if your an expert programmer looking at this, you might already see my problem. (and thank you for gracing this post with your presence, expert programmer.)
because everything in the if statement is still read and executed like normal, my program turned length into -1, instead of checking to see if it was -1. and because it succeeded in doing this, it went into the if statement, where it returned 0.
The fixed code looks like this:
Code:
if(length == -1) //If end of file is reached //Originally forgot to put == instead of =, causing nothing to print
return 0; //return 0.
While this fixed this part of the program, I had an issue with my program where it now read ALL of the notes in the text file. I began checking my work, and yet another 30 minutes into it, I found the culprit.
You see, my program finds the Password in the file by doing a
Code:
while(user_pass != note_pass)
Statement. This is the full part of that.
Code:
while(note_pass != pass) {
if(read(fd, ¬e_pass, 4) != 4) //if end of file reached
return -1;
if(read(fd, &byte, 1) != 1) //if end of file reached
return -1;
} //test closer
byte = length = 0;
while(byte != '\n') {
if(read(fd, &byte, 1) != 1) //if Byte is not read
return -1; //return end of file code
length++;
}
// printf("[DEBUG] pass = %d note_pass = %d\n", pass, note_pass);
// printf("[DEBUG] Note found! length = %d\n", length);
lseek(fd, length * -1, SEEK_CUR);
return length;
// } //old end of while. included the return length statement, so it returned the note no matter the pass. New end of while is at test closer
as you can see by the commented out curly bracket, My old end of the while statement was past the return command, so no matter what the password was, it printed.
If anyone wants to see this is closer detail, I put the new source code attached, so you can look at the final product.